package com.yanqu.road.server.cmd.zhaoshang;


import com.yanqu.road.entity.zhaoshang.ZsTopOfRank;
import com.yanqu.road.logic.pb.ZhaoShangPb;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.zhaoshang.ZhaoShangProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.zhaoshang.ZsMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;

@Cmd(code = Protocol.S_ZS_PROJECT_TOP_OF_RANK, desc = "榜首")
public class ZsWeekTopOfRankCmd extends AbstractCommand {

    private int rankType;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ZhaoShangProto.ZsWeekRankTopReqMsg reqMsg = ZhaoShangProto.ZsWeekRankTopReqMsg.parseFrom(packet.getBytes());
        rankType = reqMsg.getRankType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        int preWeekId = ZsMgr.getPreWeekId();
        Map<Integer, ZsTopOfRank> zsTopOfRankMap =  ZsMgr.getTopRankMap(preWeekId);
        ZhaoShangProto.ZsWeekRankTopRespMsg.Builder respMsg = ZhaoShangProto.ZsWeekRankTopRespMsg.newBuilder();
        if(null != zsTopOfRankMap){
            if(zsTopOfRankMap.containsKey(rankType) && rankType != 0){
                RankProto.TopRankTemp.Builder rankTopTemp = RankProto.TopRankTemp.newBuilder();
                RankProto.RankTempMsg.Builder rankTempMsg = ZhaoShangPb.parseRankTempMsg(zsTopOfRankMap.get(rankType));
                rankTopTemp.setType(rankType);
                rankTopTemp.setRankTemp(rankTempMsg);
                respMsg.addRankTopList(rankTopTemp);
            }else{
                for(int type : zsTopOfRankMap.keySet()){
                    RankProto.TopRankTemp.Builder rankTopTemp = RankProto.TopRankTemp.newBuilder();
                    RankProto.RankTempMsg.Builder rankTempMsg = ZhaoShangPb.parseRankTempMsg(zsTopOfRankMap.get(type));
                    rankTopTemp.setType(type);
                    rankTopTemp.setRankTemp(rankTempMsg);
                    respMsg.addRankTopList(rankTopTemp);
                }
            }
        }
        respMsg.setWeekId(preWeekId);
        player.sendPacket(Protocol.U_ZS_PROJECT_TOP_OF_RANK, respMsg);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
